import $ from 'jquery';
// import {ElMessage} from "element-plus";
//一個user類，一個全局變量

export default{
    state : {
        userId: -1,
        username : "",
        email: "",
        password: "",//仅仅在login页面零时存在，离开立马清空
        photo : "",
        token: "",
        score: 0,
        is_login: false,
        pulling_info : true,//判斷是否正在服務器拉取信息。
        login_url : "http://10.70.128.53:7090/api/auth/login",
        getInfo_url:"http://10.70.128.53:7090/api/user/getUserInfo",
        friends: [],//好友
        friendRequest: [],//好友请求

    },
    getters: {
    },
    mutations: {

        updateUser(state,user){
            console.log("user",user)
            state.userId = user.data.userId;
            state.username = user.data.username;
            state.photo = user.data.avatarUrl;
            state.email = user.data.email;
            state.is_login = user.is_login;
            state.score = user.data.score;

        },
        updatePhoto(state,avatarUrl){
            state.photo = avatarUrl;
        },
        updateUsername(state,username){
            state.username = username;
        },

        updateToken(state, token){
            state.token = token;
        },
        logout(state){
            state.userId = "";
            state.username = "";
            state.photo = "";
            state.token = "";
            state.is_login = false;
        },

        updatePullingInfo(state, logo){
            state.pulling_info = logo;
        },
        updateEmail(state,email){
            state.email = email;
        },
        updatePassword(state,password){
            state.password = password;
        },
        updateFriends(state,friends){
            state.friends = friends;
        },
        updateFriendRequest(state,friendRequest){
            state.friendRequest = friendRequest;
            console.log(friendRequest);
        }
    },
    actions: {
        login(context,data){//这里的context只用来启动muta里的函数
            
            $.ajax({
                type: "post",
                url: context.state.login_url,
                contentType: "application/json", // 设置请求头
                data: JSON.stringify({
                    email:data.username,
                    password:data.password,
                }),
                
                success(resp) {
                    if(resp.status === "success"){//如果成功了
                        localStorage.setItem("jwt_token",resp.data.token);
                        context.commit("updateToken",resp.data.token)
                        data.success(resp);
                    }else {
                        data.error(resp);
                    }
                },
                error(resp){
                data.error(resp);
                }
            });
            
        },

        getInfo(context, data){
            // console.log(context.state.token)
            $.ajax({
                url: context.state.getInfo_url,
                type:"get",
                headers:{
                  Authorization: "Bearer " + context.state.token//授权
                },
                success(resp){
                    if(resp.status === "success"){
                        context.commit("updateUser",{
                            ...resp,
                            is_login:true,
                          })
                        if(resp.token){
                            context.commit("updateToken",resp.token);
                            localStorage.setItem("jwt_token",resp.token);//登录更新token
                        }

                          data.success(resp);
                    }
                    else{
                        data.error(resp);
                    }
                  
                },
                error(resp){
                  data.error(resp);
                }
            })
        },
        logout(context){
            localStorage.removeItem("jwt_token");
            context.commit("logout");
        },
        getFriendRequest(context,data){
            $.ajax({
                url: `http://10.70.128.53:7090/api/friend/request/list/${context.state.userId}`,
                type: 'GET',
                headers: {
                    Authorization: `Bearer ${context.state.token}`,
                },
                success(resp) {

                    if (resp.status === "success") {
                        context.commit("updateFriendRequest",resp.data) // 更新用户列表
                        data.success(resp);
                    }else{
                        data.error(resp);
                    }
                },
                error(resp) {
                    console.error(resp);
                    data.error("网络错误，请刷新");
                },
            })
        },
        getFriends(context,data){
            $.ajax({
                url: `http://10.70.128.53:7090/api/friend/list/${context.state.userId}`,
                type: 'GET',
                headers: {
                    Authorization: `Bearer ${context.state.token}`,
                },
                success(resp) {

                    if (resp.status === "success") {
                        context.commit("updateFriends",resp.data) // 更新用户列表
                        data.success(resp);
                    }else{
                        data.error(resp);
                    }
                },
                error(resp) {
                    console.error(resp);
                    data.error("网络错误，请刷新");
                },
            });
        }

    },

    modules: {
    }
}